package engine;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class StopWord {
    public static final Charset ENCODING = StandardCharsets.ISO_8859_1;
    
    protected String filePath;
    protected ArrayList<String> stopWordList;
    
    private boolean listFilled;
    
    public StopWord(String _filePath){
        this.filePath = _filePath;
        
        this.stopWordList = null;
        this.listFilled = false;
    }
    
    public ArrayList<String> getStopWordList(){
        if (stopWordList == null){
            try{
                fillStopWordList();
            }
            catch(Exception e){
                stopWordList = null;
                System.out.println("Echec de recupération des stop word : " + e.getLocalizedMessage());
                this.listFilled = true;
            }
        }
        return this.stopWordList;
    }
    
    private void fillStopWordList() throws IOException{
        if (this.listFilled) return;
        
        this.stopWordList = new  ArrayList<String>();
        
        Path path = Paths.get(this.filePath);
        try (Scanner scanner = new Scanner(path, ENCODING.name())) {
            while (scanner.hasNextLine()) {

                this.stopWordList.add(scanner.nextLine());
            }
        }
        
        this.listFilled = true;
    }
    
    public void print(){
        for(String sw : getStopWordList()){
            System.out.println(sw);
        }
    }
}
